package cn.iocoder.yudao.module.picturebook.controller.admin.audiorecord;

import cn.iocoder.yudao.module.picturebook.controller.admin.audio.vo.AudioVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.picturebook.controller.admin.audiorecord.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.audiorecord.AudioRecordDO;
import cn.iocoder.yudao.module.picturebook.service.audiorecord.AudioRecordService;

@Tag(name = "绘本系统 - 绘本音频记录")
@RestController
@RequestMapping("/picturebook/audio-record")
@Validated
public class AudioRecordController {

    @Resource
    private AudioRecordService audioRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本全音频")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-record:create')")
    public CommonResult<Integer> createAudioRecord(@Valid @RequestBody AudioRecordSaveReqVO createReqVO) {
        return success(audioRecordService.createAudioRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本全音频")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-record:update')")
    public CommonResult<Boolean> updateAudioRecord(@Valid @RequestBody AudioRecordSaveReqVO updateReqVO) {
        audioRecordService.updateAudioRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本全音频")
    @Parameter(name = "bookId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:audio-record:delete')")
    public CommonResult<Boolean> deleteAudioRecord(@RequestParam("bookId") Integer bookId) {
        audioRecordService.deleteAudioRecord(bookId);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本全音频")
                @PreAuthorize("@ss.hasPermission('picturebook:audio-record:delete')")
    public CommonResult<Boolean> deleteAudioRecordList(@RequestParam("ids") List<Integer> ids) {
        audioRecordService.deleteAudioRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本全音频")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-record:query')")
    public CommonResult<AudioRecordRespVO> getAudioRecord(@RequestParam("id") Integer id) {
        AudioRecordDO audioRecord = audioRecordService.getAudioRecord(id);
        return success(BeanUtils.toBean(audioRecord, AudioRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本全音频分页")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-record:query')")
    public CommonResult<AudioVO> getAudioRecordPage(@Valid AudioRecordPageReqVO pageReqVO,@RequestHeader("token") Long userId) {
        AudioVO pageResult = audioRecordService.getAudioRecordPage(pageReqVO,userId);
        return success(pageResult);
    }

    //添加绘本音频阅读记录
    @Operation(summary = "添加绘本音频阅读记录")
    @PostMapping("/addAudioRecord")
    public CommonResult<Boolean> addAudioRecord(@Valid @RequestBody AudioRecordSaveReqVO createReqVO,@RequestHeader("token") Long userId) {
        audioRecordService.createAudioRecordByUser(createReqVO,userId);
        return success(true);
    }
}